//
//  STUserLoginRequestDM.m
//  STShoppingMall
//
//  Created by apple on 2018/9/15.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZB_UserLoginRequestDM.h"
#import "ZB_LoginUserDM.h"

@implementation ZB_UserLoginRequestDM
/**
 用户登录接口
 mobile:密码登录和验证码登录时，所需的手机号
 password: 密码登录时，所需的登录密码
 type :string  1：密码登录 2：验证码登录 3：微信登录 4：QQ登录 5：微博登录
 code:验证码
 第三方登录
 nickname/head_img/openid
 */
- (void)loginWithMobile:(NSString *)mobile password:(NSString *)password  type:(NSString *)type code:(NSString *)code openid:(NSString *)openid nickname:(NSString *)nickname headImg:(NSString *)headImg  compeletion:(void(^)(NSError *error))compeletion
{
    ZB_UserLoginRequestModel *requestModel = [[ZB_UserLoginRequestModel alloc]init];
    requestModel.mobile = mobile;
    requestModel.password = password;
    requestModel.is_type = type;
    requestModel.code = code;
    requestModel.is_type = type;
    requestModel.openid = openid;
    requestModel.nickname = nickname;
    requestModel.head_img = headImg;
    WS(weakSelf);
    [self sendPostWithModel:requestModel ResponseClass:[ZB_UserLoginResponseModel class] sucessResponse:^(ZB_UserLoginResponseModel *reseponseModel) {
        
        ZB_LoginUserModel *user =  [ZB_LoginUserDM currentLoginUser];
        //不同的用户，就移除之前的，保存最新的
        if(user.token != reseponseModel.data){
            [[ZB_LoginUserDM shareInstance] removeUserModel];
            [ZB_LoginUserDM removeCurrentToken];
            user = [[ZB_LoginUserModel alloc]init];
        }
        user.token = reseponseModel.data;
       
        
        user.loginStatus = ZB_LoginStatus_Login;
        
        //保存到沙盒
        [ ZB_LoginUserDM saveCurrentUserToken:user.token];
        [[ZB_LoginUserDM shareInstance] saveCurrentUserInfoToCacheWithUser:user];
        
        //回调
        [weakSelf delegateDataMangerCallBackSelector:@selector(delegate_LoginSucess) withObject:nil withAnotherObject:nil];
        
        //发布通知登录成功
        [[NSNotificationCenter defaultCenter] postNotificationName:ZB_LOGIN_SUCCESS_NOTIFATIONNAME object:nil];
        
        if(compeletion)
            compeletion(nil);
    } failResponse:^(NSError *error) {
        if(compeletion)
            compeletion(error);
    }];
}

/**
 用户注册接口
 */
- (void)login_registerAccountWithMobile:(NSString *)mobile code:(NSString *)code password:(NSString *)password passwordTo:(NSString *)passwordTo  success:(void(^)(ZB_UserRegisterResponseModel *responseModel))success fail:(void(^)(NSError *error))fail
{
    ZB_UserRegisterRequestModel *requestModel = [[ZB_UserRegisterRequestModel alloc]init];
    requestModel.mobile = mobile;
    requestModel.password = password;
    requestModel.password_confirm = passwordTo;
    requestModel.code = code;
    [self sendPostWithModel:requestModel ResponseClass:[ZB_UserRegisterResponseModel class] sucessResponse:success failResponse:fail];
}
/**
 用户发送验证码
 1：登录 2:注册 3：忘记密码 4：切换绑定
 */
- (void)login_sendCodeWithPtype:(NSString *)ptype mobile:(NSString *)mobile success:(void(^)(ZB_UserSendCodeResponseModel *responseModel))success fail:(void(^)(NSError *error))fail
{
    ZB_UserSendCodeRequestModel *requestModel = [[ZB_UserSendCodeRequestModel alloc]init];
    requestModel.mobile = mobile;
    requestModel.type = ptype;
    [self sendGetWithModel:requestModel ResponseClass:[ZB_UserSendCodeResponseModel class] sucessResponse:success failResponse:fail];
}
/**
 忘记修改密码接口
 */
- (void)login_modifyPasswordWithMobile:(NSString *)mobile code:(NSString *)code password:(NSString *)password passwordTo:(NSString *)passwordTo  success:(void(^)(ZB_UserModifyPasswordResponseModel *responseModel))success fail:(void(^)(NSError *error))fail
{
    ZB_UserModifyPasswordRequestModel *requestModel = [[ZB_UserModifyPasswordRequestModel alloc]init];
    requestModel.mobile = mobile;
    requestModel.password = password;
    requestModel.password_check = passwordTo;
    requestModel.code = code;
    [self sendPostWithModel:requestModel ResponseClass:[ZB_UserModifyPasswordResponseModel class] sucessResponse:success failResponse:fail];
}


@end
